<%--
  Created by IntelliJ IDEA.
  User: wqz
  Date: 2020/12/27
  Time: 23:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加商品</title>
</head>
<style type="text/css">
    .aa{
        width: 30%;
        margin:  0 auto;
    }
</style>
<body>
<div class="divBox">
    <div class="aa">
        <h1 align="center">添加商品</h1>
        <h3 align="center"><a href="/index/zhuye">返回首页</a></h3>
        <br>
        <h3 align="center"><a href="/index/show">搜索页</a></h3>

        <form  enctype="multipart/form-data" id="form1">
            <div>
                <label >商品名称:</label>
                <input type="text" name="name" id="name"  placeholder="请输入商品名字" autocomplete="off"  style="width: 200px">
                <span class="name" style="color:red"></span>
            </div>

            <div>
                <label >商品类型:</label>
                <input type="text" name="type" id="type"   placeholder="请输入商品类型" autocomplete="off"  style="width: 200px">
                <span class="type" style="color:red"></span>
            </div>

            <div>
                <label >商品价格:</label>
                <input type="text" name="price" id="price" placeholder="请输入商品价格" autocomplete="off"  style="width: 200px">
                <span class="price" style="color:red"></span>
            </div>

            <div>
                <label >出版时间:</label>
                <input type="text" name="publish_time" id="publish_time"   placeholder="请输入出版时间" autocomplete="off"  style="width: 200px">
            </div>
            <div>
                <label >商品描述：</label>
                <input type="text" name="description" id="description"   placeholder="请输入商品描述" autocomplete="off"  style="width: 200px">
            </div>
            <!--
            <div>
                <label >上传头像图片:</label>
                <input type="file" name="file"   id="file" >
                <span class="file" style="color:red"></span>
            </div>-->

            <div>
                <input type="button" name="login" id="login" value="提交" />&nbsp<input type="reset" value="重置"></p>
            </div>
        </form>
    </div>
</div>
</body>

<script type="text/javascript" src="/js/jquery-1.11.1.min.js" ></script>
<script>
    $(function(){
        $("#login").click(function(){
            var name=$('#name').val();
            var type=$('#type').val();
            var price=$('#price').val();
            var publish_time=$('#publish_time').val();
            var description=$('#description').val();
            //获得上传信息
            //var file = $('input[name="file"]').prop('files');

            console.log(name);
            console.log(type);
            console.log(price);

            //console.info(file);

            console.log(publish_time);
            console.log(description);
            // 邮箱正则匹配
            //var reg=/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-_.])+[A-Za-z\d]{2,4}$/;
            // 定义一个变量  默认为true
            var flag=true;
            if(type == '' || type == null|| type==undefined) {
                $(".type").html("商品类型不能为空！");
                flag=false;
            //} else if(!reg.test(email)){
            //    $(".email").html("请输入正确格式！");
            //    flag=false;
            }else{
                $(".type").html("");
            }
            if(name == '' || name == null|| name==undefined) {
                $(".name").html("商品名称不能为空！");
                flag=false;
            }else{
                $(".name").html("");
            }
            if(price == '' || price == null||price==undefined) {
                $(".price").html("商品价格不能为空！");
                flag=false;
            }else{
                $(".price").html("");
            }

            //if(file.length>0) {
            //   $(".file").html("");
            //}else{
            //    $(".file").html("头像必须上传！");
            //    flag=false;
            //}

            if(flag==true){
                console.log("是否成功。。。");
                //var formData = new FormData($("#form1")[0]);
                //formData.append("name",name);
                //formData.append("type",type);
                //formData.append("price",price);
                //formData.append("publish_time",publish_time);
                //formData.append("description",description);
                //console.log(formData);
                var formData = {"name":name,
                                "type":type,
                                "price":price,
                                "publish_time":publish_time,
                                "description":description
                                };
                console.log(formData);
                console.log(JSON.stringify(formData));
                $.ajax({
                    type:'POST',
                    url:'/commodity/addCommodity',
                    data:JSON.stringify(formData),
                    dataType: "json",
                    // async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success:function (data) {
                        if(data.code=='200'){
                            alert(data.message);
                            location.href='/index/zhuye';
                        }
                        else{
                            alert(data.message);
                        }
                    }
                })
            }

        });
    });
</script>
</html>
